<?php

namespace app\index\model;

use app\model\Model;

class Sitemap extends Model
{
  public $xml;
  public $db;

  function __construct()
  {
    $this->db = $GLOBALS['db'];
    //"always", "hourly", "daily", "weekly", "monthly", "yearly" and "never"
    $changefreq = "weekly";
    $this->xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$this->xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
  $res = $GLOBALS['db']->getAll("SELECT * FROM detail ORDER BY d_order ASC,id DESC LIMIT 0,5000");
  foreach ($res as $val) {
  $this->xml .= ' <url>' . PHP_EOL . ' <loc>' . $GLOBALS['cms']['domain'] . '/' . htmlspecialchars(d_url($val['id'])) . '</loc>' . PHP_EOL . ' <lastmod>' . local_date("Y-m-d H:i:s", $val['d_date']) . '</lastmod>' . PHP_EOL . ' <changefreq>' . $changefreq . '</changefreq>' . PHP_EOL . ' <priority>0.8</priority>' . PHP_EOL . ' </url>' . PHP_EOL;
  }
  $this->xml .= '</urlset>';
}

function index()
{
$fp = fopen("sitemap.xml", "w+");
fwrite($fp, $this->xml);
fclose($fp);
header('Location: /sitemap.xml');
}

function __destruct()
{
}
}